﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace INFTUR
{
    public partial class ManterCliente : System.Web.UI.Page
    {
        public static bool inserir = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public void limpar()
        {
            TBcpf.Text = "";
            TBemail.Text = "";
            TBendereco.Text = "";
            TBnome.Text = "";
            TBtelefone.Text = "";
            Lerro.Text = "";

            TBcpf.Enabled = false;
            TBemail.Enabled = false;
            TBendereco.Enabled = false;
            TBnome.Enabled = false;
            TBtelefone.Enabled = false;

            cancelar.Enabled = false;
            gravar.Enabled = false;
        }

        protected void cancelar_Click(object sender, EventArgs e)
        {
            limpar();
        }

        protected void gravar_Click(object sender, EventArgs e)
        {
            //Grava os dados
                try
                {
                    ClienteBO cBO = new ClienteBO();

                    cBO.Cpf = TBcpf.Text;
                    cBO.Nome = TBnome.Text;
                    cBO.Endereco = TBendereco.Text;
                    cBO.Email = TBemail.Text;
                    cBO.Telefone = TBtelefone.Text;

                    ClienteBOMetodos cBOMetodos = new ClienteBOMetodos();
                    if (inserir == true)
                    {
                        cBOMetodos.Insere(cBO);
                    }
                    else
                    {
                        //cBO.Cpf = TBcpf.Text;
                        cBOMetodos.Atualiza(cBO);
                    }
                    limpar();
                }
                catch (Exception ex)
                {
                    Lerro.Text = ex.Message;
                }
                inserir = false;
                GridView1.DataBind();
            
        }

        protected void novocliente_Click(object sender, EventArgs e)
        {
            TBcpf.Enabled = true;
            TBemail.Enabled = true;
            TBendereco.Enabled = true;
            TBnome.Enabled = true;
            TBtelefone.Enabled = true;
            
            gravar.Enabled = true;
            cancelar.Enabled = true;

            inserir = true;

        }

        protected void lbAtualizar_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)(lb).Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            string cpf = linha.Cells[0].Text;

            ClienteBO cBO = new ClienteBO();
            ClienteBOMetodos cBOM = new ClienteBOMetodos();
            
            cBO = cBOM.Consulta(cpf);

            TBcpf.Text = cBO.Cpf;
            TBemail.Text = cBO.Email;
            TBendereco.Text = cBO.Endereco;
            TBnome.Text = cBO.Nome;
            TBtelefone.Text = cBO.Telefone;

            inserir = false;

            TBcpf.Enabled = false;
            TBemail.Enabled = true;
            TBendereco.Enabled = true;
            TBnome.Enabled = true;
            TBtelefone.Enabled = true;

            gravar.Enabled = true;
            cancelar.Enabled = true;
            
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lbRemover_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)(lb).Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            string cpf = linha.Cells[0].Text;

            ClienteBO cBO = new ClienteBO();
            ClienteBOMetodos cBOM = new ClienteBOMetodos();
            cBO = cBOM.Consulta(cpf);

            cBOM.Remove(cpf);
            GridView1.DataBind();
        }

    }
}
